<template>
  <div ref="searchContent" class="search-container">
    <div class="search-container-containers">
      <img src="//s0.meituan.net/bs/fe-web-meituan/fa5f0f0/img/logo.png" alt="">
      <div class="search-container-containers-right">
        <input  @keyup.enter="search" v-model="searchContent" @blur="inputBlur" @focus="inputFocus" type="text" placeholder="搜索商家或地点">
        <div @click="search" class="iconfonts">
          <span class="iconfont">&#xe60b;</span>
        </div>
        <div @mouseenter="enterList" @mouseleave="leaveList" v-show="showList" class="list">
          <div class="list-top">
            <div class="list-top-top">
              <span>最近搜索</span>
              <span @click="deleteSearchHistory" style="cursor: pointer">删除搜索历史</span>
            </div>
            <div class="list-top-bottom">
              <span v-for="(item, index) of searchHistory" :key="index">{{item}}</span>
            </div>
          </div>
          <div class="list-bottom">
            <span class="list-bottom-top">热门搜索</span>
            <div class="list-bottom-bottom">
              <span v-for="(item, index) of searchRecordTwo.hotSearch" :key="index">{{item}}</span>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'Search',
  data() {
    return {
      showList: false,
      enterLists: false,
      /** 搜索框中的内容 */
      searchContent: ''
    }
  },
  props: {
    searchHistory: {
      type: Array,
      default() {
        return []
      }
    },
    searchRecordTwo: {
      type: Object,
      default() {
        return {}
      }
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.$route.name === 'ChangeCity' ? 
        this.changeHeight() :
        ''
    })
  },
  methods: {
    /** 改变content高度 */
    changeHeight() {
      this.$refs.searchContent.style.height = '170px'
    },
    /** 删除搜索历史 */
    deleteSearchHistory() {
      this.$emit('deleteSearchHistory')
    },
    /** 搜索 */
    search() {
      this.$emit('search', this.searchContent)
    },
    /** 输入框的聚焦 */
    inputFocus() {
      this.showList = true
      this.$emit('inputFocus')
    },
    /** 输入框的失焦 */
    inputBlur() {
      if (!this.enterLists) this.showList = false
      else {
        setTimeout(() => {
          this.showList = false
        }, 300)
      }
    },
    /** 鼠标移入列表 */
    enterList() {
      this.enterLists = true
    },
    /** 鼠标移出列表 */
    leaveList() {
      this.enterLists = false
    }
  }
}
</script>

<style lang="scss" scoped>
@import 'scss/common.scss';
.search-container {
  width: 100%;
  background: #fff;
  box-shadow: 0 2px 27px 0 rgba(0,0,0,0.10);
  height: 260px;
  &-containers {
    width: 1199px;
    margin: 0 auto;
    height: 63%;
    display: flex;
    align-items: center;
    img {
      width: 140px;
      height: 55px;
      margin-right: 200px;
    }
    &-right {
      width: 600px;
      display: flex;
      height: 50px;
      position: relative; 
      input {
        border: 1px solid #E5E5E5;
        border-right: none;
        border-radius: 4px 0 0 4px;
        line-height: 100%;
        box-sizing: border-box;
        display: inline-block !important;
        padding: 17px;
        font-size: 16px;
        height: 100%;
        background: transparent;
        width: 500px;
      }
      .iconfonts {
        @include flex-center;
        width: 100px;
        background: #FFC300;
        color: #222;
        cursor: pointer;
        height: 100%;
        border-bottom-right-radius: 4px;
        border-top-right-radius: 4px;
        .iconfont {
          font-size: 30px;
          font-weight: bold;
        }
      }
      .list {
        position: absolute;
        width: 600px;
        z-index: 101;
        top: 50px;
        border-radius: 5px;
        padding: 10px 20px;
        background: #fff;
        box-shadow: 0 2px 2px 0 rgba(0,0,0,0.10);
        &-top {
          display: flex;
          flex-direction: column;
          &-top {
            display: flex;
            justify-content: space-between;
            span {
              cursor: pointer;
              &:nth-child(1) {
                font-size: 13px;
                font-weight: bold;
                color: #999;
              }
              &:nth-child(2) {
                font-size: 12px;
                color: #aaa;
              }
            }
          }
          &-bottom {
            margin-top: 10px;
            display: flex;
            flex-wrap: wrap;
            span {
              cursor: pointer;
              font-size: 13px;
              margin-right: 5px;
              &:hover {
                color: #FE8C00;
              }
            }
          }
        }
        &-bottom {
          @extend .list-top;
          margin-top: 20px;
          &-top {
            @extend .list-top-top;
            cursor: pointer;
            font-size: 13px;
            font-weight: bold;
            color: #999;
          }
          &-bottom {
            @extend .list-top-bottom;
          }
        }
      }
    }
  }
}
</style>